package com.ext.aic.components;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ext.aic.R;
import com.ext.aic.constants.EBusConstants;
import com.ext.aic.events.OnWriteBackContinueContentListener;
import com.eyinfo.android_pure_utils.ebus.EBus;
import com.eyinfo.android_pure_utils.utils.PixelUtils;
import com.lxj.xpopup.core.BottomPopupView;

public class AIContinueWriteBottomPopup extends BottomPopupView implements OnWriteBackContinueContentListener {

    private EditText aiContinueWriteEt;
    private RelativeLayout hdRl;
    private RelativeLayout interruptGenerationRl;
    private LinearLayout aiOperateLl;
    private RelativeLayout generateRl;
    private AnimatedGradientDrawable gradientDrawable = new AnimatedGradientDrawable();

    private boolean isInterruptGeneration = false;

    public AIContinueWriteBottomPopup(Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.ai_continue_writing_layout;
    }

    @Override
    protected int getMaxHeight() {
        return PixelUtils.dip2px(getContext(), 200);
    }

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate() {
        RelativeLayout usingTheRl = findViewById(R.id.using_the_rl);
        usingTheRl.setOnClickListener(this::onUsingTheRlClick);
        RelativeLayout giveUpRl = findViewById(R.id.give_up_rl);
        giveUpRl.setOnClickListener(this::onGiveUpClick);
        Button generateBtn = findViewById(R.id.generate_btn);
        generateBtn.setOnClickListener(this::onGenerateClick);
        hdRl = findViewById(R.id.hd_rl);
        interruptGenerationRl = findViewById(R.id.interrup_generation_rl);
        interruptGenerationRl.setOnClickListener(this::onInterruptGenerationClick);
        aiContinueWriteEt = findViewById(R.id.ai_continue_write_et);
        aiOperateLl = findViewById(R.id.ai_operate_ll);
        generateRl = findViewById(R.id.generate_rl);
        aiContinueWriteEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                generateBtn.setEnabled(!TextUtils.isEmpty(s.toString()));
            }
        });

    }

    public void onInterruptGenerationClick(View view) {
        isInterruptGeneration = true;
        gradientDrawable.stop();
        generateRl.setBackgroundResource(R.drawable.ai_image_bg);
        generateRl.setVisibility(View.GONE);
        hdRl.setVisibility(View.GONE);
    }

    public void onGenerateClick(View view) {
        hdRl.setVisibility(View.VISIBLE);
        interruptGenerationRl.setVisibility(View.VISIBLE);
        aiOperateLl.setVisibility(View.GONE);
        generateRl.setVisibility(View.VISIBLE);
        float radiusInPx = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                8,
                getResources().getDisplayMetrics()
        );
        gradientDrawable.setCornerRadius(radiusInPx);
        generateRl.setBackground(gradientDrawable);
        gradientDrawable.start();

        isInterruptGeneration = false;

        String prompt = aiContinueWriteEt.getText().toString();
        EBus.getInstance().post(EBusConstants.aiWritePrompt, prompt);
    }

    public void onGiveUpClick(View v) {
        aiContinueWriteEt.setText("");
        EBus.getInstance().post(EBusConstants.giveUpAiWriteGenerate);
        dismiss();
    }

    public void onUsingTheRlClick(View v) {
        String prompt = aiContinueWriteEt.getText().toString();
        EBus.getInstance().post(EBusConstants.insertContinueContent, prompt);
        aiContinueWriteEt.setText("");
        dismiss();
    }

    @Override
    public void onWriteBack(String content) {
        if (!isInterruptGeneration) {
            aiContinueWriteEt = findViewById(R.id.ai_continue_write_et);
            aiContinueWriteEt.setText(content);
        }
        interruptGenerationRl.setVisibility(View.GONE);
        aiOperateLl.setVisibility(View.VISIBLE);
        generateRl.setVisibility(View.GONE);
        gradientDrawable.stop();
        generateRl.setBackgroundResource(R.drawable.ai_image_bg);
    }
}
